//
//  MSCountControl.m
//  Weshare
//
//  Created by KevinCao on 16/4/22.
//  Copyright © 2016年 wxwl. All rights reserved.
//

#import "MSCountControl.h"

@interface MSCountControl ()<UITextFieldDelegate>
@property UIButton *minusBtn;
@property UIButton *addBtn;
@property UILabel  *valueLabel;
@property UITextField *valueTextField;
@end

@implementation MSCountControl

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.minusBtn.backgroundColor = [UIColor clearColor];
        self.minusBtn.frame = CGRectMake(self.width/2.0-58, self.height/2.0-20, 40, 40);
        [self.minusBtn setImage:[UIImage imageNamed:@"order_minus_disabled"] forState:UIControlStateDisabled];
        [self.minusBtn setImage:[UIImage imageNamed:@"order_minus_nor"] forState:UIControlStateNormal];
        [self.minusBtn addTarget:self action:@selector(minusAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.minusBtn];
        
        self.addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.addBtn.backgroundColor = [UIColor clearColor];
        self.addBtn.frame = CGRectMake(self.width/2.0+18, self.height/2.0-20, 40, 40);
        [self.addBtn setImage:[UIImage imageNamed:@"order_add_disabled"] forState:UIControlStateDisabled];
        [self.addBtn setImage:[UIImage imageNamed:@"order_add_nor"] forState:UIControlStateNormal];
        [self.addBtn addTarget:self action:@selector(addAction) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.addBtn];
        
        self.valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.width/2.0-18, 0, 36, self.height)];
        self.valueLabel.textAlignment = NSTextAlignmentCenter;
        self.valueLabel.textColor = kOrangeColor;
        self.valueLabel.font = [UIFont systemFontOfSize:22];
//        [self addSubview:self.valueLabel];
        
        self.valueTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.width/2.0-18, 0, 36, self.height)];
        self.valueTextField.enabled = NO;
        self.valueTextField.delegate = self;
        self.valueTextField.textColor = kOrangeColor;
        self.valueTextField.font = [UIFont systemFontOfSize:22];;
        self.valueTextField.textAlignment = NSTextAlignmentCenter;
        self.valueTextField.keyboardType = UIKeyboardTypeNumberPad;
        [self.valueTextField addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
//        self.valueTextField.backgroundColor = kDarkGrayColor;
        [self addSubview:self.valueTextField];

        self.minValue = 1;
        self.maxValue = 12;
        self.minusBtn.enabled = NO;
    }
    return self;
}

#pragma mark - actions
- (void)minusAction
{
    if (!self.valueTextField.text.length) {
        self.value = 1;
    }
    self.value--;
    if (_enableEdit) {
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
}

- (void)addAction
{
    if (!self.valueTextField.text.length) {
        self.value = 0;
    }
    self.value++;
}

#pragma mark - setters
- (void)setEnable:(BOOL)enable
{
    _enable = enable;
    if (enable) {
        self.minusBtn.enabled = self.value>self.minValue;
        self.addBtn.enabled = self.value<self.maxValue;
    } else {
        self.minusBtn.enabled = NO;
        self.addBtn.enabled = NO;
    }
}

- (void)setEnableEdit:(BOOL)enableEdit{
    _enableEdit = enableEdit;
    if (enableEdit) {
        self.valueTextField.enabled = YES;
        self.valueTextField.left = self.width/2.0-23;
        self.valueTextField.width = 46;
    }
    else{
        self.valueTextField.enabled = NO;
    }
}

- (void)setMinValue:(NSInteger)minValue
{
    _minValue = minValue;
    _value = minValue;
//    self.valueLabel.text = [NSString stringWithFormat:@"%ld",minValue];
    self.valueTextField.text = [NSString stringWithFormat:@"%ld",minValue];
}

- (void)setMaxValue:(NSInteger)maxValue {
    _maxValue = maxValue;
    self.minusBtn.enabled = self.value>self.minValue;
    self.addBtn.enabled = self.value<self.maxValue;
}

- (void)setValue:(NSInteger)value
{
    _value = value;
    if (_enableEdit) {
        if (value != 0) {
            self.valueTextField.text = [NSString stringWithFormat:@"%ld",value];
            self.minusBtn.enabled = value>self.minValue;
            self.addBtn.enabled = value<self.maxValue;
            [self sendActionsForControlEvents:UIControlEventValueChanged];
        }
        else{//等于0
            self.valueTextField.text = [NSString stringWithFormat:@"%ld",value];
        }
    }
    else{
        self.valueTextField.text = [NSString stringWithFormat:@"%ld",value];
        self.minusBtn.enabled = value>self.minValue;
        self.addBtn.enabled = value<self.maxValue;
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
//    self.valueLabel.text = [NSString stringWithFormat:@"%ld",value];
}

- (void)textFieldEditingChanged:(UITextField *)textField{
    NSInteger kValue = [textField.text integerValue];
    if (kValue > 0) {
        NSInteger phoneNumLength = (textField.text.length < 3) ? textField.text.length : 3;
        textField.text = [textField.text substringWithRange:NSMakeRange(0, phoneNumLength)];
        self.value = [textField.text integerValue];
    }
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
    if (!textField.text.length) {
        self.value = 1;
    }
    return YES;
}

@end
